
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author karenAndy
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;

public class Set<T> implements InterfaceSet<T> {

    private Tree23 setA = new Tree23();
    private Tree23 setB = new Tree23();
    private Tree23 setC = new Tree23();

    public Integer[] Arrange(String tree23) {

        if (tree23.equalsIgnoreCase("A")) {

            String treeA = ShowSet("A");
            treeA = treeA.replaceAll(" ", ",");

            while (treeA.contains(",,")) {
                treeA = treeA.replaceAll(",,", ",");
            }
            treeA = treeA.substring(1);

            String[] tempTreeA = treeA.split(",");
            Integer[] tempA = new Integer[tempTreeA.length];

            for (int i = 0; i < tempTreeA.length; i++) {
                tempA[i] = Integer.parseInt(tempTreeA[i].toString());
            }

            return tempA;
        } else {
            String treeB = ShowSet("B");
            treeB = treeB.replaceAll(" ", ",");

            while (treeB.contains(",,")) {
                treeB = treeB.replaceAll(",,", ",");
            }
            treeB = treeB.substring(1);

            String[] tempTreeB = treeB.split(",");
            Integer[] tempB = new Integer[tempTreeB.length];

            for (int i = 0; i < tempTreeB.length; i++) {
                tempB[i] = Integer.parseInt(tempTreeB[i]);
            }

            return tempB;
        }
    }

    public String ShowSet(String set) {

        if (set.equals("A")) {
            return setA.Show();
        } else if (set.equals("B")) {
            return setB.Show();
        } else if (set.equals("C")) {
            return setC.Show();
        } else {
            return "";
        }
    }

    public void Union() {
        setC = new Tree23();
        Integer[] Atree = Arrange("A");
        Integer[] Btree = Arrange("B");
        LinkedList setList = new LinkedList();
        ArrayList<Integer> temporalArray = new ArrayList<Integer>();

        for (int i = 0; i < Atree.length; i++) {
            setList.add(Atree[i]);
        }
        for (int j = 0; j < Btree.length; j++) {
            if (setList.contains(Btree[j])) {
                j++;
            } else {
                setList.add(Btree[j]);
            }
        }

        Collections.sort(setList);
        for (int k = 0; k < setList.size(); k++) {
            setC.Add(setList.get(k));
        }
    }

    public void Intersection() {
        setC = new Tree23();
        Integer[] Atree = Arrange("A");
        Integer[] Btree = Arrange("B");
        LinkedList setList = new LinkedList();

        for (int i = 0; i < Atree.length; i++) {
            for (int j = 0; j < Btree.length; j++) {

                if (Atree[i] == Btree[j]) {
                    setList.add(Atree[i]);
                }
            }
        }
        for (int k = 0; k < setList.size(); k++) {
            setC.Add(setList.get(k));
        }
    }

    public void Add(T set, T element) {
        if (set.toString().equals("A")) {
            setA.Add(element);
        } else if (set.toString().equals("B")) {
            setB.Add(element);
        }
    }

    public Tree23 Remove(T set, T element) { 
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

